package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqConfirmMobile;
import com.lanlian.commonlib.bean.ReqSmsCodeForUpdateMobile;
import com.lanlian.commonlib.bean.RespSmsCode;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.TimeCount;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 13:26
 *     desc   : 修改邮箱
 *     version: 1.0
 * </pre>
 */
public class UpdateEmailActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvMobile;
    private TextView mTvCode;
    private EditText mEtCode;
    private TextView mTvNext;

    private TimeCount mTimeCount;
    private String mValKey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_email, true);
        initView();
        setListeners();
        if (mDBManager.getUser() != null) {
            mTvMobile.setText(mDBManager.getUser().getMobile());
        }
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("修改邮箱");
        mTvMobile = findViewById(R.id.tv_mobile);
        mTvCode = findViewById(R.id.tv_code);
        mEtCode = findViewById(R.id.et_code);
        mTvNext = findViewById(R.id.tv_next);
        mTimeCount = new TimeCount(this, 60000, 1000, mTvCode);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvCode.setOnClickListener(this);
        mTvNext.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvNext);
        worksSizeCheckUtil.addAllEditText(mEtCode);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvNext.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvNext.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_code:
                //获取验证码
                ReqSmsCodeForUpdateMobile reqSmsCodeForUpdateMobile = new ReqSmsCodeForUpdateMobile();
                reqSmsCodeForUpdateMobile.setOperation("email");
                reqSmsCodeForUpdateMobile.setType("oldMobile");
                mTvCode.setClickable(false);
                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(), reqSmsCodeForUpdateMobile,
                        new INetWorkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                RespSmsCode respSmsCode = (RespSmsCode) response;
                                mValKey = respSmsCode.getValKey();
                                mTimeCount.startTime();
                                showToast("验证码已发送到你的手机");
                            }

                            @Override
                            public void onFail(BaseResponse baseResponse) {
                                mTvCode.setClickable(true);
                                showToast(baseResponse.getMsg());
                            }
                        });
                break;
            case R.id.tv_next:
                //跳转下一步
                ReqConfirmMobile reqConfirmMobile = new ReqConfirmMobile();
                reqConfirmMobile.setValKey(mValKey);
                reqConfirmMobile.setInfoType("mobile");
                reqConfirmMobile.setCode(mEtCode.getText().toString());
                showLoadingDialog();
                VolleyRequestUtils.getInstance().confirmMobile(this.toString(), reqConfirmMobile, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        RespSmsCode respSmsCode = (RespSmsCode) response;
                        startActivity(new Intent(UpdateEmailActivity.this, UpdateEmailNextActivity.class)
                                .putExtra("verifyCode", respSmsCode.getVerifyCode()));
                        finish();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mTimeCount.cancel();
        mTimeCount = null;
        super.onDestroy();
    }

}
